<?php
    /*
     * 1.服务器需要开启curl
     * 2.服务器需要放system命令
     * 3.服务器类别自己定义
     * 4.将urlinfo导入数据库中，修改数据库信息
     * 5.nginx需要新增配置如下,用于自动跳转,可参考nginx.conf文件
     */
ignore_user_abort(); // 后台运行
set_time_limit(0); // 取消脚本运行时间的超时上限
date_default_timezone_set('PRC');
    /**
     * 检查域名
     * @param $domain
     * @return bool true=被封，false=正常
     */
    function checkdomain($domain){
        // 2.调用api检测 --根据实际的购买api进行更改
        $url='http://vip.xxweixin.com/weixin/wx_domain.php?user=403585363f&key=4c660aa326c965bbcb74e5ee7122ed79&domain='.$domain.'/f';//api地址
        $ch = curl_init();
        curl_setopt ($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        $file_contents = json_decode(curl_exec($ch),true);
        curl_close($ch);
        error_log(date("Y-m-d H:i:s =6= ").json_encode($file_contents).PHP_EOL,3,__DIR__."/shell.log");
        return 2 == $file_contents['status'];
    }

	function checkRecord($domain){
        $url = "http://".$domain;
		$ch = curl_init();
		$timeout = 5;
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
		$contents = curl_exec($ch);
		curl_close($ch);
		
		if(strpos($contents,'aliyun') !==false){
			error_log(date("Y-m-d H:i:s =8= ").'已掉备案'.PHP_EOL,3,__DIR__."/shell.log");
			return true;
		}else{
			error_log(date("Y-m-d H:i:s =7= ").'备案检测正常'.PHP_EOL,3,__DIR__."/shell.log");
			return false;
		}
	}

    $type = 'A';
    $table = 'urlinfo_107';
    $db_host = 'rm-wz97m7f230mzxryh52o.mysql.rds.aliyuncs.com:3306';//数据库地址
    $db_username = 'proadmin';//数据库帐号
    $db_pwd = 'Xiuxian2018';//数据库密码
    $db_database = 'xiuxian';//数据库名称
    $application_config = __DIR__.'/../application/config/config.php';

    while(!file_exists('close')){
        //foreach (["A","B"] as $idx=>$type) {

            $connect = mysqli_connect($db_host, $db_username, $db_pwd, $db_database);
            if (mysqli_connect_errno()) {
                error_log(date("Y-m-d H:i:s ") . "=9= 数据库连接失败" . PHP_EOL, 3, __DIR__ . "/shell.log");
                usleep(2100000);
                continue;
            }

            $sql = "select * from $table where status = 0 and type = '" . $type . "'";//服务器类别自己定义
            $result = mysqli_query($connect, $sql);
            if (!$result) {
                error_log(date("Y-m-d H:i:s ") . " =8= 找不到当前使用域名" . PHP_EOL, 3, __DIR__ . "/shell.log");
                usleep(2100000);
                continue;
            }
            $row = mysqli_fetch_array($result, MYSQLI_ASSOC);

            //1.获取当前使用域名
            $old_url = $row['domain'];
            error_log(date("Y-m-d H:i:s ") . "=1= 当前域名 " . json_encode($row['domain']) . PHP_EOL, 3, __DIR__ . "/shell.log");
            //2.当域名被封时操作
            $check_url = $type == "B"? $old_url."/f" : $old_url;
            if (checkdomain($check_url)||checkRecord($check_url)) {
                error_log(date("Y-m-d H:i:s ") . " =2= " . $old_url . "域名被封 " . PHP_EOL, 3, __DIR__ . "/shell.log");
                //3.获取可用新地址
                $sql = "select * from $table where status = 1 and deleted != 1 and type = '" . $type . "'";
                $result = mysqli_query($connect, $sql);
                $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
                $new_url = $row['domain'];
                if (empty($new_url)) {
                    error_log(date("Y-m-d H:i:s ") . "=3= 新域名为空 " . json_encode($row) . PHP_EOL, 3, __DIR__ . "/shell.log");
                    usleep(2500000);
                    //短信通知
                    continue;
                }
                error_log(date("Y-m-d H:i:s ") . "=4= 新域名 " . $new_url . PHP_EOL, 3, __DIR__ . "/shell.log");
                //5.删除旧地址
                $sql = "update $table set deleted = 1 , status = 1 where domain = '" . $old_url . "'";
                mysqli_query($connect, $sql);
                $sql = "update $table set status = 0 where domain = '" . $new_url . "'";
                mysqli_query($connect, $sql);


                $app_data = file_get_contents($application_config);
                $data_new = str_replace($old_url, $new_url, $app_data);
                file_put_contents($application_config, $data_new);
                error_log(date("Y-m-d H:i:s ") . "=5= 替换域名 " . $old_url . "=>" . $new_url . PHP_EOL, 3, __DIR__ . "shell.log");
            }
            usleep(2100000);
        //}
        mysqli_close($connect);
        //usleep(2100000);
    }

    exit();
?>
